<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>电子商城</title>
<link href="styles/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<%
  int flag = (int)request.getSession().getAttribute("flag");
  if (flag == 3)
    out.print("<script>alert('登录成功')</script>");
  else if (flag == 26)
    out.print("<script>alert('删除成功')</script>");
  else if (flag == 27)
    out.print("<script>alert('删除失败')</script>");
  else if (flag == 29)
    out.print("<script>alert('修改成功')</script>");
  else if (flag == 30)
    out.print("<script>alert('添加成功')</script>");
  request.getSession().setAttribute("flag", 0);
%>
<div class="main">



<div class="top">
  <p><a href="goodsManageList.jsp">公告管理</a> |
    <a href="<c:url value="/GoodsList"/>">商品管理</a> |
    <a href="<c:url value="/MemberManageList"/>">会员管理</a>  |
    <a href="<c:url value="/OrderManageList"/>">订单管理</a> |
    <a href="manage1.jsp">退出后台</a>
  </p>
</div>
  <div class="adm_con">
    <div class="bg_3">
      <div class="bg_1">
        <div class="bg_2">
          <h3><img src="images/25.gif" class="flo_right" /><img src="images/23.gif" class="flo_left" /><span>商品列表</span></h3>
          <div class="padd">

            <span id="spanFirst">第一页</span> <span id="spanPre">上一页</span> <span id="spanNext">下一页</span> <span
                  id="spanLast">最后一页</span> 第<span id="spanPageNum"></span>页/共<span id="spanTotalPage"></span>页

            <p>[ 商品列表 ] [ <a href="<c:url value="/GoodsAdd"/>">添加商品信息</a>]</p>
            <table width="100%" border="0" cellspacing="0" cellpadding="0" id="table1">
              <tr>
                <th scope="col" width="230px"> 商品名称 </th>
                <th scope="col" width="150px"> 价格 </th>
                <th scope="col" width="320px"> 发布日期 </th>
                <th scope="col" width="70px"> 修改 </th>
                <th scope="col" width="80px"> 删除 </th>
              </tr>
            </table>
              <table width="100%" border="0" cellspacing="0" cellpadding="0" id="table2">
                <c:forEach items="${GoodsList}" var="G">
                  <tr>
                    <form action="<c:url value="/DeleteGoods"/>" method="post">
                      <input name="goodsId" value="${G.id}" style="display:none"/>
                      <td width="230px"><a href="goodsManageView.jsp?goodsId=${G.id}">${G.name}</a> </td>
                      <td width="150px">${G.price}</td>
                      <td width="320px"><fmt:formatDate value="${G.createTime}" pattern="yyyy-MM-dd"/> </td>
                      <td width="70px"><img src="images/34.gif" onclick="window.location.href='/GoodsUpdate?goodsId=${G.id}'" style="border:0; cursor:hand;"/></td>
                      <td width="80px"><input type="submit" value="删除" style="border:0; cursor:hand;"/></td>
                    </form>
                  </tr>
                </c:forEach>


            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</body>
</html>

<script>
  var theTable = document.getElementById("table2");
  var totalPage = document.getElementById("spanTotalPage");
  var pageNum = document.getElementById("spanPageNum");
  var spanPre = document.getElementById("spanPre");
  var spanNext = document.getElementById("spanNext");
  var spanFirst = document.getElementById("spanFirst");
  var spanLast = document.getElementById("spanLast");
  var numberRowsInTable = theTable.rows.length;
  var pageSize = 10;
  var page = 1;

  //下一页
  function next() {

    hideTable();

    currentRow = pageSize * page;
    maxRow = currentRow + pageSize;
    if (maxRow > numberRowsInTable) maxRow = numberRowsInTable;
    for (var i = currentRow; i < maxRow; i++) {
      theTable.rows[i].style.display = '';
    }
    page++;

    if (maxRow === numberRowsInTable) { nextText(); lastText(); }
    showPage();
    preLink();
    firstLink();
  }

  //上一页
  function pre() {

    hideTable();
    page--;

    currentRow = pageSize * page;
    maxRow = currentRow - pageSize;

    if (currentRow > numberRowsInTable) currentRow = numberRowsInTable;
    for (var i = maxRow; i < currentRow; i++) {
      theTable.rows[i].style.display = '';
    }


    if (maxRow === 0) {
      preText(); firstText();
    }
    showPage();
    nextLink();
    lastLink();
  }

  //第一页
  function first() {
    hideTable();
    page = 1;
    for (var i = 0; i < pageSize; i++) {
      theTable.rows[i].style.display = '';
    }
    showPage();
    preText();
    if (page === pageCount()) nextText();
    else nextLink();
    lastLink();
  }

  //最后一页
  function last() {
    hideTable();
    page = pageCount();
    currentRow = pageSize * (page - 1);
    for (var i = currentRow; i < numberRowsInTable; i++) {
      theTable.rows[i].style.display = '';
    }
    showPage();
    nextText();
    if (pageCount() === 1) preText();
    else preLink();
    firstLink();
  }

  function hideTable() {
    for (var i = 0; i < numberRowsInTable; i++) {
      theTable.rows[i].style.display = 'none';
    }
  }

  function showPage() {
    pageNum.innerHTML = page;
  }

  //总共页数
  function pageCount() {
    var count = 0;
    if (numberRowsInTable % pageSize !== 0) count = 1;
    return parseInt(numberRowsInTable / pageSize) + count;
  }

  //显示链接
  function preLink() { spanPre.innerHTML = "<a href='javascript:pre();'>上一页</a>"; }
  function preText() { spanPre.innerHTML = "上一页"; }

  function nextLink() { spanNext.innerHTML = "<a href='javascript:next();'>下一页</a>"; }
  function nextText() { spanNext.innerHTML = "下一页"; }

  function firstLink() { spanFirst.innerHTML = "<a href='javascript:first();'>第一页</a>"; }
  function firstText() { spanFirst.innerHTML = "第一页"; }

  function lastLink() { spanLast.innerHTML = "<a href='javascript:last();'>最后一页</a>"; }
  function lastText() { spanLast.innerHTML = "最后一页"; }

  //隐藏表格
  function hide() {
    for (var i = pageSize; i < numberRowsInTable; i++) {
      theTable.rows[i].style.display = 'none';
    }
    totalPage.innerHTML = pageCount();
    pageNum.innerHTML = '1';
    if (pageCount() !== 1) nextLink();
    firstLink();
    lastLink();
  }
  hide();
</script>
